package com.ruoyi.wechat.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wechat.mapper.CampsiteMapper;
import com.ruoyi.wechat.domain.Campsite;
import com.ruoyi.wechat.service.ICampsiteService;

/**
 * 营地营位Service业务层处理
 * 
 * @author 马传
 * @date 2024-07-26
 */
@Service
public class CampsiteServiceImpl implements ICampsiteService 
{
    @Autowired
    private CampsiteMapper campsiteMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询营地营位
     * 
     * @param campsiteId 营地营位主键
     * @return 营地营位
     */
    @Override
    public Campsite selectCampsiteByCampsiteId(String campsiteId)
    {
        Campsite campsite = campsiteMapper.selectCampsiteByCampsiteId(campsiteId);
        if(campsite !=null){
            Map<Integer, String> factorDescriptions = getFactorDescriptions();
            if(campsite.getFactor() !=null){
                List<Integer> factors = Arrays.stream(StringUtils.split(campsite.getFactor(), ","))
                        .map(Integer::valueOf)
                        .collect(Collectors.toList());
                List<String> fa = factors.stream()
                        .map(factorDescriptions::get)
                        .collect(Collectors.toList());
                campsite.setRefactor(fa);
            }

        }

        return campsite;
    }



    /**
     * 查询营地营位列表
     * 
     * @param campsite 营地营位
     * @return 营地营位
     */
    @Override
    public List<Campsite> selectCampsiteList(Campsite campsite)

    {
        //将字符串按,分隔传递处理
        if (StringUtils.isNotBlank(campsite.getFactor())) {
            List<Integer> factors = Arrays.stream(StringUtils.split(campsite.getFactor(), ","))
                    .map(Integer::valueOf)
                    .collect(Collectors.toList());
            campsite.setFactors(factors);
        }
        Map<Integer, String> factorDescriptions = getFactorDescriptions();
        //搜索
        List<Campsite> campsites = campsiteMapper.selectCampsiteList(campsite);

            //将字典按条件返回
            for (Campsite campsiteone : campsites) {
                if(campsiteone.getFactor() != null){
                List<Integer> factors = Arrays.stream(StringUtils.split(campsiteone.getFactor(), ","))
                        .map(Integer::valueOf)
                        .collect(Collectors.toList());
                List<String> fa = factors.stream()
                        .map(factorDescriptions::get)
                        .collect(Collectors.toList());
                campsiteone.setRefactor(fa);
                }
            }


        return campsites;

    }
    /**
     * 获取营地营位描述信息
     * @return
     */
    private Map<Integer, String> getFactorDescriptions(){
        //查询数据字典，获取所有营地营位条件
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("c_campsite_factor");
        List<SysDictData> factorList = dictDataMapper.selectDictDataList(sysDictData);

        Map<Integer, String> factorDescriptions = factorList.stream()
                .collect(Collectors.toMap(
                        factor -> Integer.parseInt(factor.getDictValue()),
                        factor -> factor.getDictLabel()
                ));
        return factorDescriptions;
    }
    /**
     * 新增营地营位
     * 
     * @param campsite 营地营位
     * @return 结果
     */
    @Override
    public int insertCampsite(Campsite campsite)
    {
        //随机id
        campsite.setCampsiteId(IdUtils.randomUUID());
        return campsiteMapper.insertCampsite(campsite);
    }

    /**
     * 新增营地营位
     *
     * @param campsite 营地营位
     * @return 结果
     */
    @Override
    public int insertCampsites(Campsite[] campsite)
    {
        //随机id
        for (Campsite campsite1 : campsite) {
            campsite1.setCampsiteId(IdUtils.randomUUID());
            campsiteMapper.insertCampsite(campsite1);
        }
        return 1;
    }

    /**
     * 修改营地营位
     * 
     * @param campsite 营地营位
     * @return 结果
     */
    @Override
    public int updateCampsite(Campsite campsite)
    {
        return campsiteMapper.updateCampsite(campsite);
    }

    /**
     * 批量删除营地营位
     * 
     * @param campsiteIds 需要删除的营地营位主键
     * @return 结果
     */
    @Override
    public int deleteCampsiteByCampsiteIds(String[] campsiteIds)
    {
        return campsiteMapper.deleteCampsiteByCampsiteIds(campsiteIds);
    }

    /**
     * 删除营地营位信息
     * 
     * @param campsiteId 营地营位主键
     * @return 结果
     */
    @Override
    public int deleteCampsiteByCampsiteId(String campsiteId)
    {
        return campsiteMapper.deleteCampsiteByCampsiteId(campsiteId);
    }
}
